package cn.book.admin.ctrl;

import cn.book.admin.entity.BookInfo;
import cn.book.admin.entity.Page;
import cn.book.admin.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
public class BookController {
    @Autowired
    private BookService bookService;

    @RequestMapping("bookSearch")
    public ModelAndView search(@RequestParam(name = "name", required = false) String name, @RequestParam(name = "pageNo", defaultValue = "1") int pageNo, @RequestParam(name = "pageSize", defaultValue = "5") int pageSize) {
        if(pageNo<1)
            pageNo=1;

        Page<BookInfo> search = bookService.search(name, pageSize, pageNo);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("booklist");//视图名称
        mv.addObject("p", search);//塞 数据
        //mv.addObject("name",name);//
        return mv;
    }
//    @RequestMapping("/bookSearch2")
//    public String search2(Model m, @RequestParam(name="name",required = false) String name, @RequestParam(name="pageNo",defaultValue = "1") int pageNo, @RequestParam(name="pageSize",defaultValue = "5") int pageSize){
//        Page<BookInfo> search = bookService.search(name, pageSize, pageNo);
//        m.addAttribute("p",search);
//        return "booklist";
//    }
//    @RequestMapping("delbook")
//    public void del(@RequestParam("id")long id, PrintWriter out){
//        int r=this.bookService.del(id);
//
//       // CharacterEncodingFilter c=null;
//        out.print(r>0?"<script>alert('delete success');location.replace('bookSearch.do');</script>":"<script>alert('delete failure');history.back();</script>");
//    }

    @RequestMapping("delbook")
    public String del(@RequestParam("id") long id) {
        int r = this.bookService.del(id);
        if (r > 0)
            return "delok";

        return "delfail";

        // CharacterEncodingFilter c=null;
        //out.print(r>0?"<script>alert('delete success');location.replace('bookSearch.do');</script>":"<script>alert('delete failure');history.back();</script>");
    }

    @RequestMapping("bookToAdd")
    public String toAdd(Model m) {
        m.addAttribute("types", this.bookService.findTypes());
        return "addBook";
    }

    @RequestMapping("addbook")
    public void add(@ModelAttribute BookInfo b, HttpServletResponse resp) throws Exception {
        int r = bookService.add(b);
        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().print(r > 0 ? "<script>alert('增加成功');location.replace('bookSearch.do');</script>" : "<script>alert('增加失败');history.back();</script>");

    }

    @RequestMapping("toBookEdit")
    public ModelAndView toEdit(@RequestParam("id") long id) {
        BookInfo bookInfo = bookService.get(id);
        ModelAndView mv = new ModelAndView();
        mv.addObject("types", this.bookService.findTypes());

        mv.setViewName("bookEdit");
        mv.addObject("book", bookInfo);
        return mv;
    }

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
        // true:允许输入空值，false:不能为空值 }
    }
}
